<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * date: 2021-12-04 11:45:41
 * @LastEditors: Sam
 * @LastEditTime: 2021-12-04 12:34:52
 */

namespace app\admin\controller;

use think\facade\Db;

class Message extends Base
{

    public function index()
    {
        if (IS_POST) {
            $search = request()->param();
            $where = [];
            if (isset($search["name"]) && $search["name"] != "") $where[] = ["name", "like", '%'.$search["name"].'%'];
            $db = Db::name('message')->where($where)->order('id desc');
            $list = $db->page($search['page'], $search['limit'])->select()->toArray();
            if (count($list)) {
                foreach ($list as &$v) {
                    $v['status'] = table_switch('Message/is_show', ['id' => $v['id']], $v['status'] == 1 ? true : false, '已读|未读');
                    $v['time'] = date('Y-m-d H:i:s', $v['time']);
                }
            }

            admin_response(0, 'success', $list, $db->count());
        }
        return $this->render_table([
            "page" => true,
            "toolbar_row" => [
                table_button('admin/Message/del', "删除", "confirm")
            ],
            "cols" => [
                ["type" => "checkbox"],
                ["field" => "id", "title" => "ID", "align" => "center"],
                ["field" => "name", "title" => "名字", "align" => "center"],
                ["field" => "tel", "title" => "电话", "align" => "center"],
                ["field" => "email", "title" => "邮箱", "align" => "center"],
                ["field" => "message", "title" => "消息", "align" => "center"],
                ["field" => "status", "title" => "状态", "align" => "center"],
                ["field" => "time", "title" => "留言时间", "align" => "center"],
            ],
            "where" => [
                "name" => ["type" => "input", "title" => "名字"],
            ]

        ], false);

    }

    public function is_show()
    {
        $id = param_check('id');
        $checked = param_check('checked', 0);
        $res = Db::name('message')->where('id', $id)->update([
            'status' => $checked ? 1 : 0
        ]);
        $res ? json_response(1, '修改成功') : json_response(0, '修改失败');
    }

    /**
     * 删除banner
     * date 2020/8/13 17:55
     */
    public function del()
    {
        $id = param_check('id');
        if (!$banner = Db::name('message')->where('id', '=', $id)->find()) json_response(0, '不存在');
        if ($banner->delete()) json_response(1, '操作成功');
        json_response(0, '操作失败');
    }


}